define(function(require ,exports ,module){
    var util = require("/core/util");
    util.namespace("blog4node.admin.article.controller");
	
	function findList($scope ,ServicesProvider ,$rootScope){
		var cols = [
			{
				width : '40px',
				displayName : '序号',
				cellTemplate : '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{((pagingOptions.currentPage - 1) * pagingOptions.pageSize) + row.rowIndex + 1}}</span></div>'
			},
			{field:'title', displayName:'title'},
			{field:'author', displayName:'author'},
			{field:'content', displayName:'content'},
			{field:'pub_date', displayName:'pub_date'},
			{field:'hidden', displayName:'hidden'},
			{field:'meta.original', displayName:'original'},
			{field:'meta.recommended', displayName:'recommended'},
			{field:'meta.votes', displayName:'votes'},
			{
				field:'_id',
				displayName : '操作',
				cellTemplate : '/core/tpl/EditCellTemplate.html'
			}
		];
		$scope.filter = 'all';
		$scope.$watch('filter',function(newVal ,oldVal){
			if(newVal != oldVal){
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
			}
		})
		$scope.totalServerItems = 0;
		$scope.pagingOptions = {
			pageSizes: [20, 50, 100],
			pageSize: 20,
			currentPage: 1
		};
		$scope.setPagingData = function(data, page, pageSize){
			$scope.totalServerItems = data.total;
			$scope.data = data.page;
			if (!$scope.$$phase) {
				$scope.$apply();
			}
		};
		$scope.getPagedDataAsync = function (limit, start, searchText) {
			ServicesProvider.article.get({
				limit : limit,
				start : start,
				filter : $scope.filter
			},function (result) {
				$scope.setPagingData(result ,result.start ,result.limit);
			});
		};

		$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

		$scope.$watch('pagingOptions', function (newVal, oldVal) {
			//console.log(arguments ,newVal !== oldVal && newVal.currentPage !== oldVal.currentPage);
			if (newVal !== oldVal) {
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
			}
		}, true);
		$scope.filterOptions = { filterText: '', useExternalFilter: true };
		$scope.gridOptions = {
			columnDefs : cols,
			data : 'data',
			multiSelect : false,
			enablePaging: true,
			showFooter: true,
			totalServerItems: 'totalServerItems',
			filterOptions : $scope.filterOptions,
			pagingOptions: $scope.pagingOptions
		};


		//-------------
		$scope.cellEdit = function(_id){
			event.stopPropagation();
			console.log('cellEdit' ,_id );
		}
		$scope.cellDelete = function(_id){
			event.stopPropagation();
			ServicesProvider.article.delete({_id : _id},function(){
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            });
		}
	}

	function findDraftList($scope ,ServicesProvider ,$rootScope){
		var cols = [
			{
				width : '40px',
				displayName : '序号',
				cellTemplate : '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{((pagingOptions.currentPage - 1) * pagingOptions.pageSize) + row.rowIndex + 1}}</span></div>'
			},
			{field:'title', displayName:'title'},
			{field:'author', displayName:'author'},
			{field:'content', displayName:'content'},
			{field:'pub_date', displayName:'pub_date'},
			{field:'hidden', displayName:'hidden'},
			{field:'meta.original', displayName:'original'},
			{field:'meta.recommended', displayName:'recommended'},
			{field:'meta.votes', displayName:'votes'},
			{
				field:'_id',
				displayName : '操作',
				cellTemplate : '/core/tpl/EditCellTemplate.html'
			}
		];
		$scope.filter = 'all';
		$scope.$watch('filter',function(newVal ,oldVal){
			if(newVal != oldVal){
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
			}
		})
		$scope.totalServerItems = 0;
		$scope.pagingOptions = {
			pageSizes: [20, 50, 100],
			pageSize: 20,
			currentPage: 1
		};
		$scope.setPagingData = function(data, page, pageSize){
			$scope.totalServerItems = data.total;
			$scope.data = data.page;
			if (!$scope.$$phase) {
				$scope.$apply();
			}
		};
		$scope.getPagedDataAsync = function (limit, start, searchText) {
			ServicesProvider.article.get({
				limit : limit,
				start : start,
				filter : $scope.filter
			},function (result) {
				$scope.setPagingData(result ,result.start ,result.limit);
			});
		};

		$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

		$scope.$watch('pagingOptions', function (newVal, oldVal) {
			//console.log(arguments ,newVal !== oldVal && newVal.currentPage !== oldVal.currentPage);
			if (newVal !== oldVal) {
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
			}
		}, true);
		$scope.filterOptions = { filterText: '', useExternalFilter: true };
		$scope.gridOptions = {
			columnDefs : cols,
			data : 'data',
			multiSelect : false,
			enablePaging: true,
			showFooter: true,
			totalServerItems: 'totalServerItems',
			filterOptions : $scope.filterOptions,
			pagingOptions: $scope.pagingOptions
		};


		//-------------
		$scope.cellEdit = function(_id){
			event.stopPropagation();
			console.log('cellEdit' ,_id );
		}
		$scope.cellDelete = function(_id){
			event.stopPropagation();
			console.log('cellDelete' ,_id);
		}
	}

	function findTrashList($scope ,ServicesProvider ,$rootScope){
		var cols = [
			{
				width : '40px',
				displayName : '序号',
				cellTemplate : '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{((pagingOptions.currentPage - 1) * pagingOptions.pageSize) + row.rowIndex + 1}}</span></div>'
			},
			{field:'title', displayName:'title'},
			{field:'author', displayName:'author'},
			{field:'content', displayName:'content'},
			{field:'pub_date', displayName:'pub_date'},
			{field:'hidden', displayName:'hidden'},
			{field:'meta.original', displayName:'original'},
			{field:'meta.recommended', displayName:'recommended'},
			{field:'meta.votes', displayName:'votes'},
			{
				field:'_id',
				displayName : '操作',
				cellTemplate : '/core/tpl/EditCellTemplate.html'
			}
		];
		$scope.filter = 'all';
		$scope.$watch('filter',function(newVal ,oldVal){
			if(newVal != oldVal){
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
			}
		})
		$scope.totalServerItems = 0;
		$scope.pagingOptions = {
			pageSizes: [20, 50, 100],
			pageSize: 20,
			currentPage: 1
		};
		$scope.setPagingData = function(data, page, pageSize){
			$scope.totalServerItems = data.total;
			$scope.data = data.page;
			if (!$scope.$$phase) {
				$scope.$apply();
			}
		};
		$scope.getPagedDataAsync = function (limit, start, searchText) {
			ServicesProvider.article.get({
				limit : limit,
				start : start,
				filter : $scope.filter
			},function (result) {
				$scope.setPagingData(result ,result.start ,result.limit);
			});
		};

		$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

		$scope.$watch('pagingOptions', function (newVal, oldVal) {
			//console.log(arguments ,newVal !== oldVal && newVal.currentPage !== oldVal.currentPage);
			if (newVal !== oldVal) {
				$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
			}
		}, true);
		$scope.filterOptions = { filterText: '', useExternalFilter: true };
		$scope.gridOptions = {
			columnDefs : cols,
			data : 'data',
			multiSelect : false,
			enablePaging: true,
			showFooter: true,
			totalServerItems: 'totalServerItems',
			filterOptions : $scope.filterOptions,
			pagingOptions: $scope.pagingOptions
		};


		//-------------
		$scope.cellEdit = function(_id){
			event.stopPropagation();
			console.log('cellEdit' ,_id );
		}
		$scope.cellDelete = function(_id){
			event.stopPropagation();
		}
	}
	
    var controllers = {
        findList : ['$scope' ,'ServicesProvider','$rootScope' ,findList],
		findDraftList : ['$scope' ,'ServicesProvider','$rootScope' ,findDraftList],
		findTrashList : ['$scope' ,'ServicesProvider','$rootScope' ,findTrashList]
    };
    return blog4node.admin.article.controller = controllers;
})